import {
  _decorator,
  animation,
  CCInteger,
  Component,
  find,
  instantiate,
  Node,
  Prefab,
} from "cc";
import { Plant } from "./Plant";
import { Sun } from "./Sun";
const { ccclass, property } = _decorator;

@ccclass("SunFlower")
export class SunFlower extends Plant {
  @property(CCInteger)
  produceDuration: number = 5;

  @property(Prefab)
  sunPrefab: Prefab = null;

  private produceTimer: number = 0;

  anim: animation.AnimationController = null;

  private delataTime: number;

  protected onLoad(): void {
    this.anim = this.getComponent(animation.AnimationController);
    this.anim.setValue("isGlowing", false);
  }

  update(deltaTime: number): void {
    super.update(deltaTime);
    this.delataTime = deltaTime;
  }

  enableUpdate() {
    super.enableUpdate();
    this.produceTimer += this.delataTime;
    if (this.produceTimer >= this.produceDuration) {
      this.anim.setValue("isGlowing", true);
      this.produceTimer = 0;
    }
  }
  /**
   * 生产阳光
   */
  produceSun() {
    const sunNode = instantiate(this.sunPrefab);
    sunNode.parent = find("Canvas/ForeGround");
    sunNode.setPosition(this.node.position);
    sunNode.getComponent(Sun).jumpTo();
  }
}
